package pe.com.bn.sach.mantenimiento.controller;

 
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import pe.com.bn.sach.service.SelectService;
import pe.com.bn.sach.domain.Bnchf08Ubigeo;

import java.util.List;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class UbigeoController extends MultiActionController {
	
    private String listar;
    
    private SelectService servicioSelect;
    
    
    private String commandName;
    private Class commandClass;
    
    public UbigeoController()
    {
        ParameterMethodNameResolver resolver = new ParameterMethodNameResolver();
        resolver.setParamName("method");
        resolver.setDefaultMethodName("cargaListaTasador");
        setMethodNameResolver(resolver);
    }

   
    public ModelAndView cargaListaProvincia(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
		ModelAndView mav = new ModelAndView("select_dependiente_proceso");
		
		Bnchf08Ubigeo ubi = new Bnchf08Ubigeo();
		String selectDestino="";
		String opcion="";
		String varUbigeo=""; 
		String f08IdProv="";
		f08IdProv=request.getParameter("f08IdProv"); 
		selectDestino=request.getParameter("select"); 
		opcion=request.getParameter("opcion"); 
		varUbigeo=request.getParameter("varUbigeo");
		
		if(opcion.trim().length()==1)
			opcion="0"+opcion;
		if(f08IdProv!=null)
		if(f08IdProv.trim().length()==1)
			f08IdProv="0"+f08IdProv;
		
		ubi.getId().setF08IdDpto(opcion);
		List l = servicioSelect.listProvincia(ubi);
		String urldistrito ="document.frmMain.selProvincia";
		urldistrito = urldistrito+varUbigeo+".value";
		
		mav.addObject("f08IdProv", f08IdProv);
		mav.addObject("selectDestino", selectDestino);
		mav.addObject("opcion", opcion);
		mav.addObject("varUbigeo", varUbigeo);
		mav.addObject("urldistrito", urldistrito);
		
		mav.addObject("listdepartamento", l);
		
		
		return mav;
       
    }
    
    public ModelAndView cargaListaDistrito(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
    	
		ModelAndView mav = new ModelAndView("select_dependiente_proceso2");
		
		Bnchf08Ubigeo ubi = new Bnchf08Ubigeo();
		String selectDestino="";
		String opcion="";
		String varUbigeo=""; 
		String iddepto="";
		String f08IdDistri="";
		selectDestino=request.getParameter("select"); 
		f08IdDistri=request.getParameter("f08IdDistri");
		opcion=request.getParameter("opcion"); 
		iddepto=request.getParameter("iddepto");
		varUbigeo=request.getParameter("varUbigeo"); 
		if(opcion.trim().length()==1)
			opcion="0"+opcion;
		if(iddepto.trim().length()==1)
			iddepto="0"+iddepto;
		if(f08IdDistri!=null)
		if(f08IdDistri.trim().length()==1)
		f08IdDistri="0"+f08IdDistri;
		ubi.getId().setF08IdProv(opcion);
		ubi.getId().setF08IdDpto(iddepto);
		List l = servicioSelect.listDistrito(ubi);
		mav.addObject("f08IdDistri",f08IdDistri);		
		mav.addObject("iddepto", "0"+iddepto);
		mav.addObject("selectDestino", selectDestino);
		mav.addObject("opcion", opcion);
		mav.addObject("varUbigeo", varUbigeo);
		mav.addObject("listdepartamento", l);
		return mav;
       
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public Object getCommandObject(HttpServletRequest request)
    throws Exception
    {
    Object command = formBackingObject(request);
    bind(request, command);
    return command;
    }
    protected Object formBackingObject(HttpServletRequest request)
    throws Exception
    {
        return commandClass.newInstance();
    }

	

	public Class getCommandClass() {
		return commandClass;
	}


	public void setCommandClass(Class commandClass) {
		this.commandClass = commandClass;
	}


	public String getCommandName() {
		return commandName;
	}


	public void setCommandName(String commandName) {
		this.commandName = commandName;
	}

	public String copyString(String vari){
		if(vari == null || vari == "null" )
			vari="";
		return vari;
		
	}
	
	
	public int copyInt(String vari){
		if(vari == null || vari == "null" )
			vari="0";
		return Integer.parseInt(vari);
	}
	
	public Long copyLong(String vari){
		return new Long(vari);
	}
	
	public java.util.Date copyDate(String vari) throws ParseException{
		SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");
		java.util.Date d = null;
		if(vari == null || vari == "null" )
			vari ="10-10-2000";
			d=sdf.parse(vari);
		return d;
		
	}


	public void setServicioSelect(SelectService servicioSelect) {
		this.servicioSelect = servicioSelect;
	}

	
}


